<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理单词</title>
    <style>
        /* 全局样式 */
        body {
            font-family: 'Arial', sans-serif;
            background: linear-gradient(135deg, #f4f4f9, #e0e0e7);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
            color: #333;
        }

        h1 {
            font-size: 2rem;
            color: #6200ea;
            margin-bottom: 20px;
            text-align: center;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
        }

        /* 表单样式 */
        #word-form {
            background-color: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
            margin-bottom: 20px;
        }

        #word-form label {
            display: block;
            margin-bottom: 10px;
            font-weight: bold;
        }

        #word-form input[type="text"],
        #word-form textarea {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 2px solid #6200ea;
            border-radius: 8px;
            font-size: 1rem;
        }

        #word-form textarea {
            height: 150px;
            resize: vertical;
        }

        #word-form input[type="text"]:focus,
        #word-form textarea:focus {
            border-color: #3700b3;
            outline: none;
        }

        #word-table {
            width: 100%;
            margin-bottom: 20px;
        }

        #word-table th, #word-table td {
            padding: 10px;
            text-align: left;
        }

        #word-table th {
            background-color: #6200ea;
            color: white;
        }

        #word-table td {
            background-color: white;
        }

        #add-row {
            background-color: #6200ea;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 8px;
            cursor: pointer;
            margin-bottom: 10px;
            transition: background-color 0.3s, transform 0.3s;
        }

        #add-row:hover {
            background-color: #3700b3;
            transform: translateY(-2px);
        }

        #word-form button[type="submit"] {
            background-color: #03dac6;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 8px;
            cursor: pointer;
            transition: background-color 0.3s, transform 0.3s;
        }

        #word-form button[type="submit"]:hover {
            background-color: #018786;
            transform: translateY(-2px);
        }

        /* 已保存的分类列表 */
        #category-list {
            list-style-type: none;
            padding: 0;
            width: 100%;
            max-width: 600px;
        }

        #category-list li {
            background-color: white;
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        #category-list li button {
            background-color: #ff1744;
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s, transform 0.3s;
        }

        #category-list li button:hover {
            background-color: #d50000;
            transform: translateY(-2px);
        }

        /* 返回游戏链接 */
        #back-to-game {
            background-color: #03dac6;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 8px;
            cursor: pointer;
            text-decoration: none;
            transition: background-color 0.3s, transform 0.3s;
        }

        #back-to-game:hover {
            background-color: #018786;
            transform: translateY(-2px);
        }

        /* 手机端适配 */
        @media (max-width: 600px) {
            h1 {
                font-size: 1.5rem;
            }

            #word-form {
                padding: 15px;
            }

            #word-form input[type="text"],
            #word-form textarea {
                font-size: 0.9rem;
            }

            #add-row, #word-form button[type="submit"] {
                font-size: 0.9rem;
                padding: 8px 16px;
            }

            #category-list li {
                flex-direction: column;
                align-items: flex-start;
            }

            #category-list li button {
                margin-top: 10px;
            }
        }
    </style>
</head>
<body>
    <h1>管理单词</h1>
    <form id="word-form">
        <label for="category">分类名称:</label>
        <input type="text" id="category" name="category" required>
        <label for="bulk-words">批量添加单词（格式：中文,英文，一行一个）:</label>
        <textarea id="bulk-words" placeholder="例如：
苹果,apple
香蕉,banana"></textarea>
        <button type="submit">保存</button>
    </form>

    <h2>已保存的分类</h2>
    <ul id="category-list"></ul>

    <a href="index.html" id="back-to-game">返回游戏</a>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const wordForm = document.getElementById('word-form');
            const categoryList = document.getElementById('category-list');

            // 保存数据
            wordForm.addEventListener('submit', (event) => {
                event.preventDefault();
                const category = document.getElementById('category').value;
                const bulkWords = document.getElementById('bulk-words').value;

                // 解析批量添加的单词
                const words = [];
                bulkWords.split('\n').forEach(line => {
                    const [chinese, english] = line.trim().split(',');
                    if (chinese && english) {
                        words.push({ chinese, english });
                    }
                });

                if (words.length === 0) {
                    alert('请输入有效的单词对！');
                    return;
                }

                const categories = JSON.parse(localStorage.getItem('categories')) || {};
                categories[category] = words;
                localStorage.setItem('categories', JSON.stringify(categories));

                alert('保存成功！');
                loadCategories();
            });

            // 加载分类列表
            function loadCategories() {
                const categories = JSON.parse(localStorage.getItem('categories')) || {};
                categoryList.innerHTML = '';
                Object.keys(categories).forEach(category => {
                    const li = document.createElement('li');
                    li.innerHTML = `
                        <span>${category}</span>
                        <div>
                            <button type="button" class="edit-category" data-category="${category}">编辑</button>
                            <button type="button" class="delete-category" data-category="${category}">删除</button>
                        </div>
                    `;
                    categoryList.appendChild(li);
                });

                // 绑定编辑和删除按钮事件
                document.querySelectorAll('.edit-category').forEach(button => {
                    button.addEventListener('click', () => {
                        const category = button.dataset.category;
                        editCategory(category);
                    });
                });

                document.querySelectorAll('.delete-category').forEach(button => {
                    button.addEventListener('click', () => {
                        const category = button.dataset.category;
                        deleteCategory(category);
                    });
                });
            }

            // 编辑分类
            function editCategory(category) {
                const categories = JSON.parse(localStorage.getItem('categories')) || {};
                const words = categories[category] || [];

                // 填充表单
                document.getElementById('category').value = category;
                document.getElementById('bulk-words').value = words.map(word => `${word.chinese},${word.english}`).join('\n');
            }

            // 删除分类
            function deleteCategory(category) {
                const categories = JSON.parse(localStorage.getItem('categories')) || {};
                delete categories[category];
                localStorage.setItem('categories', JSON.stringify(categories));
                loadCategories();
            }

            // 初始化
            loadCategories();
        });
    </script>
</body>
</html>